/**
* Copyright (C) 2018-2022
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.product.rest;
import java.util.Arrays;

import co.yixiang.api.ApiResult;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.user.vo.YxUserAddressQueryVo;
import lombok.AllArgsConstructor;
import co.yixiang.modules.logging.aop.log.Log;
import co.yixiang.modules.product.domain.YxStoreSupplier;
import co.yixiang.modules.product.service.YxStoreSupplierService;
import co.yixiang.modules.product.service.dto.YxStoreSupplierQueryCriteria;
import co.yixiang.modules.product.service.dto.YxStoreSupplierDto;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import co.yixiang.domain.PageResult;
/**
* @author hupeng
* @date 2024-06-15
*/
@AllArgsConstructor
@Api(tags = "供应商管理")
@RestController
@RequestMapping("/api/yxStoreSupplier")
public class YxStoreSupplierController {

    private final YxStoreSupplierService yxStoreSupplierService;
    private final IGenerator generator;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','yxStoreSupplier:list')")
    public void download(HttpServletResponse response, YxStoreSupplierQueryCriteria criteria) throws IOException {
        yxStoreSupplierService.download(generator.convert(yxStoreSupplierService.queryAll(criteria), YxStoreSupplierDto.class), response);
    }

    @GetMapping
    @ApiOperation("查询供应商")
    @PreAuthorize("@el.check('admin','yxStoreSupplier:list')")
    public ResponseEntity<PageResult<YxStoreSupplierDto>> getYxStoreSuppliers(YxStoreSupplierQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(yxStoreSupplierService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping("/getAllYxStoreSuppliers")
    @ApiOperation(value = "查询所有供应商",notes = "")
    @PreAuthorize("@el.check('admin','yxStoreSupplier:list')")
    public ApiResult<List<YxStoreSupplier>> getUserAddressList(){
        List<YxStoreSupplier> list = (yxStoreSupplierService.list());
        return ApiResult.ok(list);
    }

    @PostMapping
    @Log("新增供应商")
    @ApiOperation("新增供应商")
    @PreAuthorize("@el.check('admin','yxStoreSupplier:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody YxStoreSupplier resources){
        return new ResponseEntity<>(yxStoreSupplierService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改供应商")
    @ApiOperation("修改供应商")
    @PreAuthorize("@el.check('admin','yxStoreSupplier:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody YxStoreSupplier resources){
        yxStoreSupplierService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除供应商")
    @ApiOperation("删除供应商")
    @PreAuthorize("@el.check('admin','yxStoreSupplier:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
        Arrays.asList(ids).forEach(id->{
            yxStoreSupplierService.removeById(id);
        });
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
